from transformers import pipeline, AutoModelWithLMHead, AutoTokenizer

import warnings

warnings.filterwarnings('ignore')

modelName = "Helsinki-NLP/opus-mt-zh-en"  # 模型名

print('正在加载翻译模型......')

model = AutoModelWithLMHead.from_pretrained(modelName)

tokenizer = AutoTokenizer.from_pretrained(modelName)

translation = pipeline('translation_zh_to_en', model=model, tokenizer=tokenizer)

print('加载完成')

while True:
    text = input('请输入需要翻译的语句:\n')

    res = translation(text)[0]['translation_text']

    print(res)
